package com.supermap.dataservice.cloud.survey;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.cloud.survey.SurveyArea;
import com.supermap.dubbo.api.cloud.survey.SurveyService;
import com.supermap.dubbo.api.cloud.survey.Surveyanswer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author aqua
 * @description
 * @date 2018年12月11 下午 3:35
 * @email 1301584033@qq.com
 */
@Service(timeout = 10000000)
public class SurveyServiceImpl implements SurveyService {

    @Resource
    private SurveyMapper surveyMapper;

    @Override
    public JSONUtil getSurveyCategoryByAreaCode(String areaCode,Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(surveyMapper.getSurveyCategoryByAreaCode(areaCode,userId));
    }

    @Override
    public JSONUtil getSurveyByCategoryId(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        SurveyDoc surveyDoc = surveyMapper.getSurveyByCategoryId(id);
        if (surveyDoc != null) {
            //遍历大类
            surveyDoc.setMenuList(surveyMapper.getSurveyMenuByContent(surveyDoc.getId()));
            if (surveyDoc.getMenuList() != null && surveyDoc.getMenuList().size() > 0) {
                for (SurveyMenu surveyMenu : surveyDoc.getMenuList()) {
                    surveyMenu.setSurveyContentList(surveyMapper.getSurveyContent(surveyMenu.getId()));
                    if (surveyMenu.getSurveyContentList() != null && surveyMenu.getSurveyContentList().size() > 0) {
                        for (SurveyContent surveyContent : surveyMenu.getSurveyContentList()) {
                            surveyContent.setOptionsList(surveyMapper.getSurveyOptions(surveyContent.getId()));
                        }
                    }
                }

            }
        }

        return new JSONUtil().setData(surveyDoc);
    }

    @Override
    public JSONUtil putSurvey(List<Surveyanswer> surveyanswerList) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(surveyMapper.putSurvey(surveyanswerList));

    }

    @Override
    public JSONUtil getSurveyByAreaCodeSMS(int areaCode, Table table) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SurveyDoc> surveyDocList = new ArrayList<>();
        if (areaCode > 0) {
            List<SurveyDoc> surveyDocs = surveyMapper.getSurveyByAreaCodeSMS(areaCode, table);
            int count = surveyMapper.getSurveyByAreaCodeCountSMS(areaCode);
            if (surveyDocs != null && !surveyDocs.isEmpty()) {
                for (SurveyDoc surveyDoc : surveyDocs) {
                    surveyDoc.setUserName(getSmsBaseUserById(surveyDoc.getUserId()));
                    surveyDocList.add(surveyDoc);
                }
                table.setCount(count);
                table.setData(surveyDocList);
            }
        }
        return jsonResult(table);
    }

    @Override
    public String getSmsBaseUserById(String id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        return surveyMapper.getSurveyByIdSms(id);
    }

    //修改
    @Override
    public JSONUtil getCountServeySmsBySendTime(String startTime, String endTime, Table table, String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SurveyDoc> surveys = surveyMapper.getServeySmsBySendTime(startTime, endTime, table, areaCode);
        int count = surveyMapper.getCountServeySmsBySendTime(startTime, endTime, table, areaCode);
        table.setCount(count);
        table.setData(surveys);
        return jsonResult(table);
    }

    @Override
    public JSONUtil getSurveryPre(String contentid, String areaCode) {
        JSONUtil jsonUtil = new JSONUtil();
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (contentid != null && !"".equals(contentid)) {
            List<SurveyDoc> list = surveyMapper.getSurveryPre(contentid, areaCode);
            jsonUtil.setMsg("success!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
        } else {
            jsonUtil.setMsg("问卷id不能小于0");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAnsSurveyUserNameBySurveyId(Table table, String surveyId, String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (surveyId != null && !"".equals(surveyId)) {
            List<SurveyDoc> list = surveyMapper.getAnsSurveyUserNameBySurveyId(table, surveyId, areaCode);
            int count = surveyMapper.getCountSurveyUserNameBySurveyId(surveyId, areaCode);
            table.setData(list);
            table.setCount(count);
        }
        return jsonResult(table);
    }

    @Override
    public JSONUtil getUserOptionByIdAndUserId(Integer id, String userId) {

        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (id > 0 && userId != null && !"".equals(userId)) {

            SurveyDoc surveyDoc = surveyMapper.getSurveyByCategoryId(id);
            if (surveyDoc != null) {
                //遍历大类
                surveyDoc.setMenuList(surveyMapper.getSurveyMenuByContent(surveyDoc.getId()));
                if (surveyDoc.getMenuList() != null && surveyDoc.getMenuList().size() > 0) {
                    for (SurveyMenu surveyMenu : surveyDoc.getMenuList()) {
                        surveyMenu.setSurveyContentList(surveyMapper.getUserOptionByIdAndUserId(surveyMenu.getId(), userId));
                        if (surveyMenu.getSurveyContentList() != null && surveyMenu.getSurveyContentList().size() > 0) {
                            for (SurveyContent surveyContent : surveyMenu.getSurveyContentList()) {
                                surveyContent.setOptionsList(surveyMapper.getSurveyOptions(surveyContent.getId()));
                            }
                        }
                    }

                }
            }
            return new JSONUtil().setData(surveyDoc);
        } else {
            return new JSONUtil().setMsg("问卷id获取Userid不能为空！");
        }
    }

    @Override
    public JSONUtil getSurveyName(String areaCode, String userId) {
        JSONUtil jsonUtil = new JSONUtil();
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        //sms获取用户权限
        String role = surveyMapper.getSmsUserRole(userId);
        if (role.equals("admin")) {
            return jsonResultList(getSurveyNames(""));
        } else if (role.equals("YWGLY")) {
            List<SurveyDoc> surveyDocList = new ArrayList<>();
            List<String> ids = surveyMapper.getSmsUserListId(areaCode);
            List<SurveyDoc> surveyDocs = getSurveyNames("");
            for (String id : ids) {
                for (SurveyDoc surveyDoc : surveyDocs) {
                    if (id == surveyDoc.getUserId()) {
                        surveyDocList.add(surveyDoc);
                    }
                }
            }
            return jsonResultList(surveyDocList);
        } else if (role.equals("NQYWRY")) {
            return jsonResultList(getSurveyNames(userId));
        }
        return jsonUtil.setMsg("没有权限！");
    }

    @Override
    public JSONUtil getSurveyMenuName(String surveyId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        JSONUtil jsonUtil = new JSONUtil();
        List<SurveyMenu> surveyMenus = surveyMapper.getSurveyMenuName(surveyId);
        if (surveyMenus.size() > 0 && !surveyMenus.isEmpty()) {

            jsonUtil.setMsg("success!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(surveyMenus);
        } else {
            jsonUtil.setMsg("数据为空!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delSurvey(String id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SurveyContent> surveyContents;
        List<SurveyOptions> surveyOptions;
        List<Surveyanswer> surveyanswers;
        JSONUtil jsonUtil = new JSONUtil();
        int surveyNum = 0;
        if (id != null && id != "" && !"".equals(id)) {
            List<SurveyMenu> surveyMenus = surveyMapper.getSurveyMenuByContent(Integer.parseInt(id));
            if (surveyMenus.size() > 0 && !surveyMenus.isEmpty()) {
                for (SurveyMenu surveyMenu : surveyMenus) {
                    surveyContents = surveyMapper.getSurveyContent(surveyMenu.getId());
                    if (!surveyContents.isEmpty() && surveyContents.size() > 0) {
                        for (SurveyContent surveyContent : surveyContents) {
                            surveyOptions = surveyMapper.getSurveyOptions(surveyContent.getId());
                            surveyanswers = surveyMapper.getSurveyAnswers(surveyContent.getId());
                            if (surveyanswers.size() > 0 && !surveyanswers.isEmpty()) {
                                for (Surveyanswer surveyanswer : surveyanswers) {
                                    surveyMapper.delSurveyAnswer(String.valueOf(surveyContent.getId()));
                                }
                            }
                            if (surveyOptions.size() > 0 && !surveyOptions.isEmpty()) {
                                for (SurveyOptions surveyOption : surveyOptions) {
                                    surveyMapper.delSurveyOptions(String.valueOf(surveyContent.getId()));
                                }
                            }
                        }
                        surveyMapper.delSurveyContent(String.valueOf(surveyMenu.getId()), "");
                    }
                }
                surveyMapper.delSurveyMenu(id, "");
            }
            surveyMapper.delSurveyArea(id);
            surveyNum = surveyMapper.delSurvey(id);
            jsonUtil.setData(surveyNum);
            jsonUtil.setMsg("删除试卷成功！");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        } else {
            jsonUtil.setMsg("删除试卷失败！试卷不存在！");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delSurveyMenu(String id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        JSONUtil jsonUtil = new JSONUtil();
        List<SurveyContent> surveyContents;
        List<SurveyOptions> surveyOptions;
        List<Surveyanswer> surveyanswers;

        if (id != null && id != "" && !"".equals(id)) {
            SurveyMenu surveyMenus = surveyMapper.getSurveyMenu(Integer.parseInt(id)).get(0);
            if (surveyMenus.getId() > 0) {

                surveyContents = surveyMapper.getSurveyContent(surveyMenus.getId());
                if (!surveyContents.isEmpty() && surveyContents.size() > 0) {
                    for (SurveyContent surveyContent : surveyContents) {
                        surveyOptions = surveyMapper.getSurveyOptions(surveyContent.getId());
                        surveyanswers = surveyMapper.getSurveyAnswers(surveyContent.getId());
                        if (surveyanswers.size() > 0 && !surveyanswers.isEmpty()) {
                            for (Surveyanswer surveyanswer : surveyanswers) {
                                surveyMapper.delSurveyAnswer(String.valueOf(surveyContent.getId()));
                            }
                        }
                        if (surveyOptions.size() > 0 && !surveyOptions.isEmpty()) {
                            for (SurveyOptions surveyOption : surveyOptions) {
                                surveyMapper.delSurveyOptions(String.valueOf(surveyContent.getId()));
                            }
                        }
                    }
                    surveyMapper.delSurveyContent(String.valueOf(surveyMenus.getId()), "");
                }

                int num = surveyMapper.delSurveyMenu("", id);
                jsonUtil.setData(num);
                jsonUtil.setMsg("删除大题成功！");
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
            }
        } else {
            jsonUtil.setMsg("删除试卷失败！试卷不存在！");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delSurveyContent(String id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        JSONUtil jsonUtil = new JSONUtil();
        List<SurveyOptions> surveyOptions;
        List<Surveyanswer> surveyanswers;
        if (id != null && id != "" && !"".equals(id)) {
            SurveyContent surveyContent = surveyMapper.getContent(Integer.parseInt(id)).get(0);
            List<SurveyContent> list = surveyMapper.getSurveyContentBigId(id, surveyContent.getMenuid());

            if (surveyContent.getId() > 0) {
                surveyOptions = surveyMapper.getSurveyOptions(surveyContent.getId());
                surveyanswers = surveyMapper.getSurveyAnswers(surveyContent.getId());

                if (surveyanswers.size() > 0 && !surveyanswers.isEmpty()) {
                    for (Surveyanswer surveyanswer : surveyanswers) {
                         surveyMapper.delSurveyAnswer(String.valueOf(surveyContent.getId()));
                    }
                }

                if (surveyOptions.size() > 0 && !surveyOptions.isEmpty()) {
                    for (SurveyOptions surveyOption : surveyOptions) {
                        surveyMapper.delSurveyOptions(String.valueOf(surveyContent.getId()));
                    }
                }

                int num = surveyMapper.delSurveyContent("", id);
                if (num > 0) {

                 for (SurveyContent scent:list) {
                        com.supermap.dubbo.api.cloud.survey.SurveyContent content = new com.supermap.dubbo.api.cloud.survey.SurveyContent();
                        if(scent.getId()>Integer.parseInt(id)){
                            int ids = Integer.parseInt(scent.getOrderid());
                            content.setId(scent.getId());
                            content.setOrderid(String.valueOf(ids-1));
                            surveyMapper.updateSurveyContent(content);
                        }
                    }
                }
                jsonUtil.setData(num);
                jsonUtil.setMsg("删除试卷成功！");
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
            }
        } else {
            jsonUtil.setMsg("删除试卷失败！试卷不存在！");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getContentAndOptions(Integer id) {

        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        JSONUtil jsonUtil = new JSONUtil();
        if (id > 0) {
            SurveyContent surveyContent = surveyMapper.getContent(id).get(0);
            if (surveyContent.getId() > 0) {
                List<SurveyOptions> options = surveyMapper.getSurveyOptions(surveyContent.getId());
                if (!options.isEmpty() && options != null) {
                    surveyContent.setOptionsList(options);
                    jsonUtil.setData(surveyContent);
                    jsonUtil.setMsg("成功！");
                    jsonUtil.setCode(200);
                    jsonUtil.setIsSuccess(true);
                } else {
                    jsonUtil.setData("");
                    jsonUtil.setMsg("数据为空！");
                    jsonUtil.setCode(200);
                    jsonUtil.setIsSuccess(true);
                }
            } else {
                jsonUtil.setData("");
                jsonUtil.setMsg("数据不存在！");
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
            }
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateSurvey(com.supermap.dubbo.api.cloud.survey.SurveyDoc surveyDoc) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        int num = surveyMapper.updateSurveycategory(surveyDoc);
        return jsonUtilUpdate(num, 1);
    }

    @Override
    public JSONUtil updateSurveyMenu(com.supermap.dubbo.api.cloud.survey.SurveyMenu surveyMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        int num = surveyMapper.updateSurveyMenu(surveyMenu);
        return jsonUtilUpdate(num, 1);
    }

    @Override
    public JSONUtil updateSurveyContentAndOptions(com.supermap.dubbo.api.cloud.survey.SurveyContent content) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (content != null && !content.getOptionsList().isEmpty() && content.getOptionsList() != null) {

            int num = surveyMapper.updateSurveyContent(content);
            List<Integer> listSize = new ArrayList<>();
            int num1 = 0;
            if (!content.getOptionsList().isEmpty() && content.getOptionsList() != null && content.getOptionsList().size() > 0) {
                for (com.supermap.dubbo.api.cloud.survey.SurveyOptions option : content.getOptionsList()) {
                    if (option.getId() != null && !"".equals(option.getId())) {
                        num1 += surveyMapper.updateSurveyOption(option);
                    } else {
                        num1 += surveyMapper.insertSurveyOption(option);
                    }

                }
            }
            if (num > 0 || num1 > 0) {
                return jsonUtilUpdate(1, 1);
            } else {
                return jsonUtilUpdate(0, 1);
            }
        } else {
            return jsonUtilUpdate(0, 1);
        }
    }

    @Override
    public JSONUtil insertSurvey(com.supermap.dubbo.api.cloud.survey.SurveyDoc surveyDoc) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        JSONUtil jsonUtil = new JSONUtil();
        int num1 = 0;
        if (surveyDoc != null) {
            com.supermap.dubbo.api.cloud.survey.SurveyDoc surveyDocid = surveyMapper.getSurveyDocNameNum(surveyDoc.getName());
            if (surveyDocid != null) {
                jsonUtil.setMsg("试卷名已经存在！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                return jsonUtil;
            } else {
                int num = surveyMapper.insertSurveycategory(surveyDoc);
                if (num > 0) {
                    com.supermap.dubbo.api.cloud.survey.SurveyDoc ids = surveyMapper.getSurveyDocNameNum(surveyDoc.getName());
                    if (surveyDoc.getSurveyAreas() != null && !surveyDoc.getSurveyAreas().isEmpty()) {
                        for (SurveyArea surveyArea : surveyDoc.getSurveyAreas()) {
                            num1 += surveyMapper.insertSurveyAreaCode(ids.getId(), surveyArea.getAreaCode());
                        }
                    }
                }
                if (num > 0 && num1 > 0) {
                    return jsonUtilUpdate(1, 2);
                } else {
                    return jsonUtilUpdate(0, 2);
                }
            }
        } else {
            return jsonUtilUpdate(0, 2);
        }
    }

    @Override
    public JSONUtil insertSurveyMenu(com.supermap.dubbo.api.cloud.survey.SurveyMenu surveyMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (surveyMenu != null) {
            int num = surveyMapper.insertSurveyMenu(surveyMenu);
            if (num > 0) {
                return jsonUtilUpdate(num, 3);
            } else {
                return jsonUtilUpdate(0, 3);
            }
        } else {
            return jsonUtilUpdate(0, 3);
        }
    }

    @Override
    public JSONUtil insertSurveyContentAndOptions(com.supermap.dubbo.api.cloud.survey.SurveyContent content) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if (content != null) {
            int num = surveyMapper.insertSurveyContent(content);
            int num1 = 0;
            if (num > 0) {
                if (content.getOptionsList() != null && !content.getOptionsList().isEmpty()) {
                    com.supermap.dubbo.api.cloud.survey.SurveyContent conten = surveyMapper.getContentId(content.getMenuid());
                    for (com.supermap.dubbo.api.cloud.survey.SurveyOptions options : content.getOptionsList()) {
                        options.setContentid(String.valueOf(conten.getId()));
                        num1 += surveyMapper.insertSurveyOption(options);
                    }
                }
            } else {
                return jsonUtilUpdate(0, 4);
            }
            if (num1 > 0 && num > 0) {
                return jsonUtilUpdate(1, 4);
            } else {
                return jsonUtilUpdate(0, 4);
            }
        } else {
            return jsonUtilUpdate(0, 4);
        }
    }

    public List<SurveyDoc> getSurveyNames(String userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SurveyDoc> list = surveyMapper.getSurveyName(userId);
        return list;
    }

    public JSONUtil jsonResultList(List<SurveyDoc> list) {
        JSONUtil jsonUtil = new JSONUtil();
        if (list.size() > 0 && !list.isEmpty()) {

            jsonUtil.setMsg("success!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
        } else {
            jsonUtil.setMsg("数据为空!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
        }
        return jsonUtil;
    }

    public JSONUtil jsonUtilUpdate(int num, int type) {
        JSONUtil jsonUtil = new JSONUtil();
        if (num > 0) {
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
            if (type == 1) {
                jsonUtil.setMsg("更新成功！");
            } else if (type == 2) {
                jsonUtil.setMsg("添加成功！");
            } else if (type == 3) {
                jsonUtil.setMsg("添加大题成功！");
            } else if (type == 4) {
                jsonUtil.setMsg("添加小题成功！");
            }

        } else {
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
            if (type == 1) {
                jsonUtil.setMsg("更新失败！");
            } else if (type == 2) {
                jsonUtil.setMsg("添加失败！");
            } else if (type == 3) {
                jsonUtil.setMsg("添加大题成功！");
            } else if (type == 4) {
                jsonUtil.setMsg("添加小题成功！");
            }

        }
        return jsonUtil;
    }

    public JSONUtil jsonResult(Table table) {
        JSONUtil jsonUtil = new JSONUtil();
        if (table.getData() != null && table.getCount() > 0) {

            jsonUtil.setMsg("success!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        } else {
            jsonUtil.setMsg("数据为空!");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
        }
        return jsonUtil;
    }
}
